﻿using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Furesoft.Styling
{
    public class Importer
    {
        public static string Import(string style)
        {
            var pattern = @"\s+@import\s+(\""|')(?<uri>((?:\\.|[^\""\\])*))\1";
            var lines = style.Split(';');

            for (int i = 0; i < lines.Length; i++)
            {
                var m = Regex.Match(lines[i], pattern);
                if (m.Success)
                {
                    var uri = m.Groups["uri"].Value;
                    lines[i] = Regex.Replace(lines[i], pattern, new WebClient().DownloadString(uri));
                }
            }

            var sb = new StringBuilder();

            foreach (var item in lines)
            {
                sb.AppendLine(item);
            }

            return sb.ToString();
        }
    }
}